var that
const db = wx.cloud.database();
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    education:'',
    images: [],
    user: {},
    work:'',
    introduce:'',
    currentInput:'',
    currentInput1:'',
    currentInput2:'',
    name:'',
    number:'',
    flat:'',
    openid:'',
  },
  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (options) {
    that = this 
    that.data.openid = app.globalData.openid,
    db.collection('apply').where({
      _openid:app.globalData.openid,
    }).get({
      success:function(res){
         console.log(res.data[0].name)
         that.data.flat=res.data[0].flat,
         that.data.name=res.data[0].name,
         that.data.number=res.data[0].number,
         that.data.education=res.data[0].education,
         that.data.work=res.data[0].work,
         that.data.introduce=res.data[0].introduce
         that.setData({
           flat:that.data.flat,
           id:that.data.id,
          name:that.data.name,
          number:that.data.number,
          education:that.data.education,
          work:that.data.work,
          introduce:that.data.introduce
         })
      }
    })
  
},
  getInput:function(e){
    that.setData({
      currentInput: e.detail.value
    })
  },
  getInput1:function(e){
    that.setData({
      currentInput1: e.detail.value
    })
  },
  getInput2:function(e){
    that.setData({
      currentInput2: e.detail.value
    })
  },
  getNumber:function(e){
    that.data.number=e.detail.value 
  },
/**
 * 
 * 清空页面 
 */
clear:function(){
  console.log("qingk")
  that.data.images= [],
  that.data.number='',
  that.data.education='',
  that.data.work='',
  that.data.introduce='',
  that.setData({
    education:'',
    work:'',
    introduce:'',
    images: [],
    number:''
  })
  console.log(that.data.currentInput)
},

  /**
   * 获取填写的内容
   */
  getTextAreaContent: function(event) {
    that.data.education = event.detail.value;
  },
  getTextAreaContent1: function(event) {
    that.data.work = event.detail.value;
  },
  getTextAreaContent2: function(event) {
    that.data.introduce = event.detail.value;
  },
 /**
   * 选择图片
   */
  chooseImage: function(event) {
    that.setData.srcVideo=""
    that.setData({
      srcVideo:""
    })
    wx.chooseImage({
      count: 6,
      success: function(res) {
        // 设置图片
        that.setData({
          images: res.tempFilePaths,
        })
        that.data.images = []
        console.log(res.tempFilePaths)
        for (var i in res.tempFilePaths) {
          // 将图片上传至云存储空间
          wx.cloud.uploadFile({
            // 指定要上传的文件的小程序临时文件路径
            cloudPath: that.timetostr(new Date()),
            filePath: res.tempFilePaths[i],
            // 成功回调
            success: res => {
              that.data.images.push(res.fileID)
            },
          })
        }
      },
    })
  },
  /**
   * 图片视频路径格式化
   */
  timetostr(time){
    var randnum = Math.floor(Math.random() * (9999 - 1000)) + 1000;
    var str = randnum +"_"+ time.getMilliseconds() + ".png";
    return str;
  },
 
  /**
   * 发布
   */
  formSubmit: function(e) {
    console.log('图片：', that.data.images)
    that.data.education = e.detail.value['input-content'];
    that.data.work = e.detail.value['input-content1'];
    that.data.introduce= e.detail.value['input-content2'];
    if(Object.keys(that.data.user).length===0){
      that.jugdeUserLogin();
    }
    else{
      if(that.data.education.trim() != '' && that.data.work.trim() != '' && that.data.introduce.trim() != '' && that.data.number.trim() != ''){
        if(that.data.flat==1){
            that.saveDataToServer();
        }
        else {
            that.saveDataToServer1();
            db.collection('apply').doc(that.data.openid)
            .update({
              data:{
                flat:1
              }
            })
        }
       
      } else{
        wx.showToast({
          icon: 'none',
          title: '每一项都需要填写喔',
        })
      }
    }
  },
  /**
   * 保存到发布集合中
   */
  saveDataToServer: function(event) {
    console.log("updata添加数据")
    const db = wx.cloud.database()
    db.collection('consultant').doc(that.data.openid)
    .update({
      // data 字段表示需新增的 JSON 数据
      data: {
        'education': that.data.education,
        'work':that.data.work,
        'introduce':that.data.introduce,
        'date': new Date(),
        'number':that.data.number,
        'images':that.data.images
      },
      success: function(res) {
        that.showTipAndSwitchTab();
      }
    }) 
  },
  saveDataToServer1: function(event) {
    console.log("增添数据")
    const db = wx.cloud.database()
    db.collection('consultant').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        '_id':app.globalData.openid,
        'education': that.data.education,
        'work':that.data.work,
        'introduce':that.data.introduce,
        'date': new Date(),
        'number':that.data.number,
        'name':that.data.name,
        'images':that.data.images,
        'time9':0,
        'time10':0,
        'time11':0,
        'time14':0,
        'time15':0,
        'time16':0,
        'time17':0,
        'time18':0,
        'time19':0,
        'time20':0,
        'time21':0,
        'time22':0,
        'time23':0,
      },
      success: function(res) {
        that.showTipAndSwitchTab();
      }
    }) 
  },
  /**
   * 添加成功添加提示，切换页面
   */
  showTipAndSwitchTab: function(event) {
    wx.showToast({
      icon:'none',
      title: '信息修改成功',
    })
    setTimeout(function() {
      wx.switchTab({
        url: '../../index/index',
      })
    }, 1500)
   
  },
  /**
   * 删除图片
   */
  removeImg: function(event) {
    var position = event.currentTarget.dataset.index;
    that.data.images.splice(position, 1);
    // 渲染图片
    that.setData({
      images: that.data.images,
    })
  },
  // 预览图片
  previewImg: function(e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      //当前显示图片
      current: that.data.images[index],
      //所有图片
      urls: that.data.images
    })
  },
  /**
   * 判断用户是否登录
   */
  jugdeUserLogin: function(e) {
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res){
              that.data.user = res.userInfo;
              that.setData({
                user:that.data.user,
              })
            }
          })
        }
      }
      
    })
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
  }
})